Page({
  data: {
    order: {},
    paymentMethods: [
      { name: '微信支付', value: 'wechat', icon: 'wechat-pay' },
      { name: '支付宝', value: 'alipay', icon: 'alipay' }
    ],
    selectedMethod: '',
    btnLoading: false
  },

  onLoad(options) {
    if (options.order) {
      try {
        const order = JSON.parse(decodeURIComponent(options.order));
        this.setData({ order: order });
      } catch (error) {
        console.error('解析订单数据出错:', error);
      }
    }
  },

  // 选择支付方式
  selectMethod(e) {
    const method = e.currentTarget.dataset.method;
    this.setData({ selectedMethod: method });
  },

  // 确认支付
  confirmPayment() {
    if (this.data.btnLoading) return;
    if (!this.data.selectedMethod) {
      wx.showToast({
        title: '请选择支付方式',
        icon: 'none'
      });
      return;
    }

    this.setData({ btnLoading: true });
    wx.showLoading({ title: '支付中...' });

    // 模拟支付过程
    setTimeout(() => {
      wx.hideLoading();
      this.setData({ btnLoading: false });
      wx.showToast({
        title: '支付成功',
        icon: 'success',
        duration: 2000,
        success: () => {
          wx.redirectTo({
            url: '/pages/my-orders/my-orders?refresh=true'
          });
        }
      });
    }, 2000);
  }
});